package com.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bean.InfoSingle;
import com.bean.Infotype;
import com.bean.User;
import com.google.gson.Gson;
import com.service.InfoService;
import com.service.LoginService;

/**
 * @ClassName: InfoController
 * @Description: 前台信息显示
 * @author 张鑫
 * @date 2019年8月20日
 *
 */


@RequestMapping("/info")
@Controller
public class InfoController {
	@Resource
	private InfoService infoservice;

	/** @Description:信息类型显示 */
	@RequestMapping("/list_infotype")
	@ResponseBody
	public String list_infotype() {
		List<Infotype> list = infoservice.list_infotype();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		return json;
	}
	
	/** @Description:信息显示 */
	@RequestMapping("/list_show")
	@ResponseBody
	public ModelAndView list_show(@RequestParam(value = "type_sign") int type_sign, HttpServletRequest request) {
		List<InfoSingle> list_charge = infoservice.list_show(type_sign);
		List<InfoSingle> list_free=new ArrayList<InfoSingle>();
		for(int i=0;i<=list_charge.size()-1;i++){
			if(Integer.parseInt(list_charge.get(i).getInfo_payfor())==0){
				list_free.add(list_charge.get(i));
				list_charge.remove(i);
			}
		}
/*		System.out.println("缴费的"+list_charge.size());
		System.out.println("未缴费的的"+list_free.size());*/
		ModelAndView mav=new ModelAndView();
		mav.addObject("list_charge", list_charge);
		mav.addObject("list_free", list_free);
		request.setAttribute("mainPage","/pages/show/listshow.jsp");
		mav.setViewName("view/IndexTemp");
		return mav;
	}
	
	/** @throws Exception 
	 * @Description:具体信息显示 */
	@RequestMapping("/show_detail")
	@ResponseBody
	public ModelAndView show_detail(@RequestParam(value = "type_sign") int type_sign,@RequestParam(value = "type_intro") String type_intro, HttpServletRequest request) throws Exception {
		List<InfoSingle> list_charge = infoservice.list_show(type_sign);
		for(int i=0;i<=list_charge.size()-1;i++){
			if(Integer.parseInt(list_charge.get(i).getInfo_payfor())==0){
				list_charge.remove(i);
			}
		}
		ModelAndView mav=new ModelAndView();
		mav.addObject("list_charge", list_charge);
		mav.addObject("type_intro", type_intro);
		request.setAttribute("mainPage","/pages/show/singleshow.jsp");
		mav.setViewName("view/IndexTemp");
		return mav;
	}
	
	/** @Description:返回主页登录 */
	@RequestMapping("/open_addinfo")
	@ResponseBody
	public ModelAndView open_addinfo(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		request.setAttribute("mainPage","/pages/show/add_info.jsp");
		mav.setViewName("view/IndexTemp");
		return mav;
	}
	
	/** @Description:返回主页登录 */
	@RequestMapping("/info_Add")
	@ResponseBody
	public ModelAndView info_Add(HttpServletRequest request,InfoSingle infosingle){
		ModelAndView mav = new ModelAndView();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		infosingle.setInfo_date(df.format(new Date()));
		int flag=infoservice.info_Add(infosingle);
		if(flag==1){
		request.setAttribute("mainPage","/pages/show/add_info.jsp");
		mav.setViewName("view/IndexTemp");
		}
		return mav;
	}
	
	/** @Description:返回主页登录 */
	@RequestMapping("/search_info")
	@ResponseBody
	public ModelAndView search_info(HttpServletRequest request,@RequestParam("search_keyword") String search_keyword, @RequestParam("search_type") String search_type,@RequestParam("search_method") String search_method){
		ModelAndView mav = new ModelAndView();
        if(search_method.equals("all")){
        	List<InfoSingle> list=infoservice.search_info(search_keyword,search_type);
        	System.out.println(list.size());
        	mav.addObject("list", list);
        	mav.addObject("search_type", search_type);
        }else{
        	List<InfoSingle> list=infoservice.search_info_like(search_keyword,search_type);
        	System.out.println(list.size());
        	mav.addObject("list", list);
        	mav.addObject("search_type", search_type);
        }
		request.setAttribute("mainPage","/pages/show/searchshow.jsp");
		mav.setViewName("view/IndexTemp");
		return mav;
	}
	
	/** @Description:列举信息显示 */
	@RequestMapping("/list_info")
	@ResponseBody
	public ModelAndView list_info(@RequestParam(value = "id") int id, HttpServletRequest request) {
		List<InfoSingle> list_charge = infoservice.list_info(id);
/*		System.out.println(list_charge.size());*/
		ModelAndView mav = new ModelAndView();
		mav.addObject("list_charge", list_charge);
		mav.addObject("id", id);
		request.setAttribute("mainPage", "/pages/admin/view/backmanageinfoshow.jsp");
		mav.setViewName("pages/admin/view/AdminTemp");
		return mav;
	}
	
	/** @Description:列举信息显示 */
	@RequestMapping(value="/set_access",produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String set_access(@RequestParam(value = "id") int id) {
		int flag= infoservice.set_access(id);
		String msg=null;
		if(flag==1){
			msg="设置成功";
		}
        return msg;
	}
	
	/** @Description:列举信息显示 */
	@RequestMapping(value="/set_charge",produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String set_charge(@RequestParam(value = "id") int id) {
		int flag= infoservice.set_charge(id);
		String msg=null;
		if(flag==1){
			msg="设置成功";
		}
        return msg;
	}
	
	/** @Description:根据不同条件查找信息显示 */
	@RequestMapping("/show_infobycon")
	@ResponseBody
	public ModelAndView show_infobycon(@RequestParam(value = "payforType") String payforType,@RequestParam(value = "stateType") String stateType,@RequestParam(value = "infoType") String infoType, HttpServletRequest request) {
		List<InfoSingle> list_charge = infoservice.show_infobycon(payforType,stateType,infoType);
		String type_intro=null;
		switch(infoType){
		case "1":
			type_intro="招聘信息";
			break;
		case "2":
			type_intro="培训信息";
			break;
		case "3":
			type_intro="房屋信息";
			break;
		case "4":
			type_intro="求购信息";
			break;
		case "5":
			type_intro="招商信息";
			break;
		case "6":
			type_intro="公寓信息";
			break;
		case "7":
			type_intro="求职信息";
			break;
		case "8":
			type_intro="家教信息";
			break;
		case "9":
			type_intro="车辆信息";
			break;
		case "10":
			type_intro="出售信息";
			break;
		case "11":
			type_intro="寻找信息";
			break;
		}
/*		System.out.println(list_charge.size());*/
		ModelAndView mav = new ModelAndView();
		mav.addObject("list_charge", list_charge);
		mav.addObject("type_intro", type_intro);
		request.setAttribute("mainPage", "/pages/admin/view/admin_checkinfo.jsp");
		mav.setViewName("pages/admin/view/AdminTemp");
		return mav;
	}
}
